package com.wind.trademark.bill.dao.impl;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.wind.trademark.bill.dao.BillDao;
import com.wind.trademark.bill.vo.BillDetailVo;
import com.wind.trademark.bill.vo.BillInfoVo;
import com.wind.trademark.bill.vo.BillVo;
import com.wind.trademark.bill.vo.BillVo4Detail;
import com.wind.trademark.bill.vo.BillVo4Fee;
import com.wind.trademark.bill.vo.CompInfoVo;
import com.wind.trademark.bill.vo.FeeVo;
import com.wind.trademark.bill.vo.TaskInfoVo;
import com.wind.trademark.common.dao.BaseDao;
import com.wind.trademark.common.vo.PageBean;
import com.wind.trademark.common.vo.PageParam;
@Repository
public class BillDaoImpl implements BillDao {
	@Autowired
	private BaseDao baseDao;
	@Override
	public PageBean queryBill(PageParam pageParam, Map<String, Object> params) {
		String sql= BillSql.queryBillSQL(params);
		PageBean pageBean=baseDao.queryPageBeanAndMapping(sql, pageParam,BillVo.class, params);
		return pageBean;
	}
	@Override
	public PageBean queryBillDetail(PageParam pageParam,
			Map<String, Object> params) {
		String sql= BillSql.queryDetailVo();
		PageBean pageBean=baseDao.queryPageBeanAndMapping(sql, pageParam,BillDetailVo.class, params);
		return pageBean;
	}
	@Override
	public void deleteBillDetails(List<Long> ids) {
		String sql= BillSql.deleteBillDetails();
		baseDao.getNamedParameterJdbcTemplate().update(sql,new MapSqlParameterSource().addValue("ids",ids));

	}
	@Override
	public void deleteFees(List<Long> ids) {
		String sql= BillSql.deleteFees();
		baseDao.getNamedParameterJdbcTemplate().update(sql,new MapSqlParameterSource().addValue("ids",ids));

	}
	@Override
	public PageBean queryFee(PageParam pageParam, Map<String, Object> params) {
		String sql= BillSql.queryFeeVo();
		PageBean pageBean=baseDao.queryPageBeanAndMapping(sql, pageParam,FeeVo.class, params);
		return pageBean;
	}
	@Override
	public List<BillVo4Fee> queryBillVo(Long taskId) {
		String sql= BillSql.queryBillVo();
		RowMapper<BillVo4Fee> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(BillVo4Fee.class);

		List<BillVo4Fee> result = baseDao.getNamedParameterJdbcTemplate().query(sql,new MapSqlParameterSource().addValue("taskId", taskId), rowMapper);
		return result;

	}
	@Override
	public List<FeeVo>  queryFeeVo(Long billId) {
		String sql= BillSql.queryFeeVo();
		RowMapper<FeeVo> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(FeeVo.class);

		List<FeeVo> result = baseDao.getNamedParameterJdbcTemplate().query(sql,new MapSqlParameterSource().addValue("billId", billId), rowMapper);
		return result;

	}
	@Override
	public List<BillVo4Fee> queryBillVoAndItsFeeVo(Long taskId) {
		String sql= BillSql.queryBillVo();
		RowMapper<BillVo4Fee> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(BillVo4Fee.class);

		List<BillVo4Fee> result = baseDao.getNamedParameterJdbcTemplate().query(sql,new MapSqlParameterSource().addValue("taskId", taskId), rowMapper);
		for(BillVo4Fee billVo:result){
			Long billId=billVo.getId();
			 List<FeeVo> feeVoes=this.queryFeeVo(billId);
			 billVo.setChildren(feeVoes);
		}
		
		return result;

	}
	@Override
	public List<BillVo4Detail> queryBillVoAndItsDetailVo(Long taskId) {
		String sql= BillSql.queryBillVo();
		RowMapper<BillVo4Detail> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(BillVo4Detail.class);

		List<BillVo4Detail> result = baseDao.getNamedParameterJdbcTemplate().query(sql,new MapSqlParameterSource().addValue("taskId", taskId), rowMapper);
		for(BillVo4Detail billVo4Detail:result){
			Long billId=Long.valueOf(billVo4Detail.getId());
			 List<BillDetailVo> billDetailVoes=this.queryDetailVo(billId);
			 for(BillDetailVo billDetailVo:billDetailVoes){
				 Double total= billVo4Detail.getAmount()+ billDetailVo.getAmount();
				 billVo4Detail.setAmount(total);
			 }
 			 billVo4Detail.setChildren(billDetailVoes);
		}
		
		return result;

	}
	private String getCurrencySymbol(String currency) {
		return "USD".equalsIgnoreCase(currency)?"$":"￥";
	}
	private List<BillDetailVo> queryDetailVo(Long billId) {
		String sql= BillSql.queryDetailVo();
		RowMapper<BillDetailVo> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(BillDetailVo.class);

		List<BillDetailVo> result = baseDao.getNamedParameterJdbcTemplate().query(sql,new MapSqlParameterSource().addValue("billId", billId), rowMapper);
		return result;

	}
	
	@Override
	public BillInfoVo getBillInfo(Long billId) {
		String billSql= BillSql.getBillInfo();
		RowMapper<BillInfoVo> billRowMapper = ParameterizedBeanPropertyRowMapper.newInstance(BillInfoVo.class);
		BillInfoVo billInfoVo = baseDao.getNamedParameterJdbcTemplate().queryForObject(billSql,new MapSqlParameterSource().addValue("billId", billId), billRowMapper);
		billInfoVo.setTotal(this.getTotalAmountByBillId(billId));
		billInfoVo.setPaid(this.getPaidAmountByBillId(billId));
		billInfoVo.setUnPaid(billInfoVo.getTotal()-billInfoVo.getPaid());
		
		String taskSql= BillSql.getTaskInfo();
		RowMapper<TaskInfoVo> taskRowMapper = ParameterizedBeanPropertyRowMapper.newInstance(TaskInfoVo.class);
		TaskInfoVo taskInfoVo = baseDao.getNamedParameterJdbcTemplate().queryForObject(taskSql,new MapSqlParameterSource().addValue("taskId", billInfoVo.getTaskId()), taskRowMapper);
		billInfoVo.setTaskInfoVo(taskInfoVo);
		
		String compSql= BillSql.getCompInfo();
		RowMapper<CompInfoVo> compRowMapper = ParameterizedBeanPropertyRowMapper.newInstance(CompInfoVo.class);
		CompInfoVo compInfoVo = baseDao.getNamedParameterJdbcTemplate().queryForObject(compSql,new MapSqlParameterSource().addValue("compId", taskInfoVo.getCompId()), compRowMapper);
		billInfoVo.setCompInfoVo(compInfoVo);
		return billInfoVo;
	}
	@Override
	public Double getTotalAmountByBillId(Long billId) {
		String totalAmountByBillIdSql= BillSql.getTotalAmountByBillId();
		List<Map<String, Object>>   list=baseDao.getSimpleJdbcTemplate().queryForList(totalAmountByBillIdSql, billId);
		if(list.isEmpty()){
			return 0.00;
		}else{
			return (Double)list.get(0).get("total");
		}

	}
	@Override
	public Double getPaidAmountByBillId(Long billId) {
		String paidAmountByBillId= BillSql.getPaidAmountByBillId();
		List<Map<String, Object>>   list=baseDao.getSimpleJdbcTemplate().queryForList(paidAmountByBillId, billId);
		if(list.isEmpty()){
			return 0.00;
		}else{
			return (Double)list.get(0).get("paid");
		}

	}

	
}
